local LBF = LibStub("LibButtonFacade",true)
if not LBF then return end

local function SkinChanged(arg, SkinID, Gloss, Backdrop, Group, Button, Colors)
	if not Group then return end
	if not ClassButtons_BF[Group] then
		ClassButtons_BF[Group] = {}
	end
	ClassButtons_BF[Group].SkinID = SkinID
	ClassButtons_BF[Group].Gloss = Gloss
	ClassButtons_BF[Group].Backdrop = Backdrop
	ClassButtons_BF[Group].Colors = Colors
end

local function SetupButtonRow(Owner, Group)	
	if ClassButtons_BF[Group] == nil then
		ClassButtons_BF[Group] = {}
	end
	
	local v = ClassButtons_BF[Group]
	LBF:Group(Owner, Group):Skin(v.SkinID, v.Gloss, v.Backdrop, v.Colors)
end

-- Adds support to everything...
local function AddSupport()
	-- Adds support for Class Buttons
	SetupButtonRow('Class Buttons', 'Vertical Bars')
	SetupButtonRow('Class Buttons', 'Horizontal Bars')
	
	for i = 1, 48 do
		local button = _G['ClassButton'..i]
		if button then LBF:Group('Class Buttons', 'Vertical Bars'):AddButton(button) end
	end
	
	for i = 49, 60 do
		local button = _G['ClassButton'..i]
		if button then LBF:Group('Class Buttons', 'Horizontal Bars'):AddButton(button) end
	end

	-- Adds support for default Blizzard frames
	SetupButtonRow('Blizzard', 'Buffs')
	--SetupButtonRow('Blizzard', 'ActionButtons')
	--SetupButtonRow('Blizzard', 'BonusActionButtons')
	SetupButtonRow('Blizzard', 'MultiBarBottomLeftButtons')
	SetupButtonRow('Blizzard', 'MultiBarBottomRightButtons')
	SetupButtonRow('Blizzard', 'MultiBarLeftButtons')
	SetupButtonRow('Blizzard', 'MultiBarRightButtons')
	SetupButtonRow('Blizzard', 'ShapeshiftButtons')
	SetupButtonRow('Blizzard', 'PetActionButtons')
	--SetupButtonRow('Blizzard', 'PossessButtons')

	for i = 1, 12 do
		--LBF:Group("Blizzard", "ActionButtons"):AddButton(_G[("ActionButton%d"):format(i)])
		--LBF:Group("Blizzard", "BonusActionButtons"):AddButton(_G[("BonusActionButton%d"):format(i)])
		LBF:Group('Blizzard', 'MultiBarBottomLeftButtons'):AddButton(_G[("MultiBarBottomLeftButton%d"):format(i)])
		LBF:Group('Blizzard', 'MultiBarBottomRightButtons'):AddButton(_G[("MultiBarBottomRightButton%d"):format(i)])
		LBF:Group('Blizzard', 'MultiBarLeftButtons'):AddButton(_G[("MultiBarLeftButton%d"):format(i)])
		LBF:Group('Blizzard', 'MultiBarRightButtons'):AddButton(_G[("MultiBarRightButton%d"):format(i)])
	end

	for i = 1, 10 do
		LBF:Group('Blizzard', 'ShapeshiftButtons'):AddButton(_G[("ShapeshiftButton%d"):format(i)])
		LBF:Group('Blizzard', 'PetActionButtons'):AddButton(_G[("PetActionButton%d"):format(i)])
	end

	--for i = 1, 2 do
	--	LBF:Group("Blizzard", "PossessButtons"):AddButton(_G[("PossessButton%d"):format(i)])
	--end

	LBF:RegisterSkinCallback('Class Buttons', SkinChanged, self)
	LBF:RegisterSkinCallback('Blizzard', SkinChanged, self)
end

-- a local, and secure hook for adding textures to buffs.
local function AddBuffs()
	LBF:Group('Blizzard', 'Buffs'):AddButton(this)
end
hooksecurefunc('BuffButton_OnLoad', AddBuffs)

-- Create a local frame for events and updates
local f = CreateFrame("Frame", "CB_BF", UIParent)
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_ENTERING_WORLD")

-- OnUpdate script which is terminated after it is ran once.
local interval, timer = 0, 0
f:SetScript('OnUpdate', function(self, elapsed)
	interval = interval - elapsed
	if interval <= 0 then

		if timer >= 0 then
			timer = timer - 1 or 0
			if timer == 0 then
				
				AddSupport()
				CB_BF:SetScript('OnUpdate', nil) -- kills this onUpdate script
			end
		end

		interval = 1
	end
end)

-- OnEvent...
f:SetScript("OnEvent", function(self, event)
	if event == "ADDON_LOADED" and arg1 == "ClassButtons" then
		if ClassButtons_BF == nil then ClassButtons_BF = {} end
		
	elseif event == "PLAYER_ENTERING_WORLD" then
		timer = 5
	end
end)
